package 常用类.Array类.list.List课后练习;

import java.util.ArrayList;
import java.util.List;

public class CommodityOperate {
    //储存所有商品的集合
    List<Commodity> commodityList=new ArrayList<>();

    //添加商品
    public void addCommodity(Commodity commodity) throws Exception {
        if (commodity==null){
            throw new Exception("商品为空");
        }
        commodityList.add(commodity);
    }

    //根据商品编号获得一个商品信息
    public Commodity getProId(int proId){
        for (Commodity commodity:commodityList){
            if (commodity.getProId()==proId){
                return commodity;
            }
        }
        return null;
    }

    //查看库存商品
    public Commodity queryAllCommodity(){
        for (Commodity commodity:commodityList){
            System.out.println(commodity);
        }
        return null;
    }

    //修改库存商品
    public Commodity upDateCommodity(Commodity commodity) throws Exception {
        Commodity commodity1=this.getProId(commodity.getProId());
        if (commodity1==null){
            throw new Exception("商品为空");
        }
        commodity1.setProId(commodity.getProId());
        commodity1.setProName(commodity.getProName());
        commodity1.setProSize(commodity.getProSize());
        commodity1.setProPrice(commodity.getProPrice());
        commodity1.setProNum(commodity.getProNum());
        return commodity1;
    }
}
